<?php
/**
 * 人机验证
 */

namespace utils;

use afs\Request\V20180112 as Afs;

class AliVerify
{
    protected $accesskey_id;
    protected $accesskey_secret;
    protected $app_key = 'FFFF0N0000000000A7D4';
    protected $scene = 'nc_other_h5';

    public function __construct()
    {
        $this->accesskey_id = 'LTAI5tLWTj4ZTKQ1kLtqiMfy';
        $this->accesskey_secret = '2PYrK1qAZtJdmPLKeVFaiWgjtVRJ21';
    }

    /**
     * 人机验证
     * @param string $app_key
     * @param string $scene
     * @param string $sessionId
     * @param string $token
     * @param string $sig
     * @return bool
     */
    public function ali_verify($app_key = '', $scene = '', $sessionId = '', $token = '', $sig = '')
    {
        if ($this->app_key != $app_key || $this->scene != $scene) return false;
        include_once __DIR__ . '/../aliyun-afs/aliyun-php-sdk-core/Config.php';
        //YOUR ACCESS_KEY、YOUR ACCESS_SECRET请替换成您的阿里云accesskey id和secret
        $iClientProfile = \DefaultProfile::getProfile("cn-hangzhou", $this->accesskey_id, $this->accesskey_secret);
        $client = new \DefaultAcsClient($iClientProfile);
        \DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "afs", "afs.aliyuncs.com");
        $request = new Afs\AuthenticateSigRequest();
        $request->setSessionId($sessionId);// 必填参数，从前端获取，不可更改，android和ios只传这个参数即可
        $request->setToken($token);// 必填参数，从前端获取，不可更改
        $request->setSig($sig);// 必填参数，从前端获取，不可更改
        $request->setScene($scene);// 必填参数，从前端获取，不可更改
        $request->setAppKey($app_key);//必填参数，后端填写
        $request->setRemoteIp(getClientIp());//必填参数，后端填写
        $response = $client->getAcsResponse($request);//返回code 100表示验签通过，900表示验签失败
        $response = $this->object_array($response);
        if ($response['Code'] == 100) { //验签成功
            return true;
        }
        return false;
    }


    /**
     * 对象转数组
     */
    public function object_array($array)
    {
        if (is_object($array)) {
            $array = (array)$array;
        }
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }

    /**
     * 生成签名
     * @param $data
     * @return string
     */
    public function get_sign($data)
    {
        unset($data['sign']);
        $str = '68j5r28xc1v21ya0a66ecef62b4edfdcc1fe' . '2';
        foreach ($data as $k => $v) {
            $str .= $k;
            if (is_array($v)) {
                foreach ($v as $kk => $vv) {
                    $str .= $kk;
                    $str .= $vv;
                }
            } else {
                $str .= $v;
            }
        }
        for ($i = 0; $i < strlen($str); $i++) {
            $temp_str = ord($str[$i]);
            $change_after[$i] = $temp_str;
        }
        sort($change_after);
        $str = '';
        for ($i = 0; $i < count($change_after); $i++) {
            $str .= chr($change_after[$i]);
        }
        $str = preg_replace('/[^a-zA-Z0-9]/s', '', $str);
        $sign_me = strtoupper(MD5($str));
        return $sign_me;
    }
}